package com.hongyuan.sellfitness.ui.message.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.activity.PostDetailsActivity;
import com.hongyuan.sellfitness.ui.find.beans.PostDetailBean;
import com.hongyuan.sellfitness.ui.message.beans.MsgDetailBean;
import com.hongyuan.sellfitness.ui.message.beans.MsgPraiseDeatailBean;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MsgPraiseDetailViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<PostDetailBean> entity = new ObservableField<>();

    public MsgPraiseDetailViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<MsgPraiseDetailItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<MsgPraiseDetailItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_msg_praise_detail);

    //去帖子详情
    public BindingCommand goCircleDetail = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("circle_id",String.valueOf(entity.get().getCircle_id()));
            startActivity(PostDetailsActivity.class,bundle);
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_CIRCLE_INFO){
            PostDetailBean bean = (PostDetailBean)dataBean;
            entity.set(bean);
        }

        if(code == Constants.GET_PRAISE_LIST){
            List<MsgPraiseDeatailBean.ListBean> mList = ((MsgPraiseDeatailBean) dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(MsgPraiseDeatailBean.ListBean bean : mList){
                    MsgPraiseDetailItemViewModel itemViewModel = new MsgPraiseDetailItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }
    }
}
